﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.DataBaseNew;
using System.ServiceModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class ClientComunication : IMessageCallback, IDisposable
    {
        IMessage pipeProxy = null;
        ForumService forumService = null;
        int forum;


        public ClientComunication(ForumService fs)
        {
            this.forumService = fs;
            initProxy();
            this.forum = 1;
            //probably need to add a call to enter a certain forum here
        }


        public void initProxy()
        {
            DuplexChannelFactory<IMessage> pipeFactory =
                  new DuplexChannelFactory<IMessage>(
                      new InstanceContext(this),
                      new NetTcpBinding(),
                      new EndpointAddress("net.tcp://localhost:8000/ISubscribe"));
            try
            {
                pipeProxy = pipeFactory.CreateChannel();
            }
            catch (Exception e)
            {
            }
        }

        public void logout(User me)
        {
            this.pipeProxy.Logout(me);
        }


        public void getUserList()
        {
            pipeProxy.getUserList();
        }

        //public void getSubForumList(Client me)
        //{
        //    if (me == null)
        //        pipeProxy.Back(new Client{ Name = "guest" + -1, Permission = 1, X = 0, Y = 0, Z = 0 });
        //    else 
        //        pipeProxy.Back(me);
        //}

        //public void getPage(Client me, int pos)
        //{
        //    pipeProxy.GetPage(me, pos, 10, 0);
        //}

        public void promote(User me, string name, string forum)
        {
            this.pipeProxy.AddNewModerator(me, name, forum);
        }

        internal void promoteManager(User me, string name, string forum)
        {
            this.pipeProxy.SetManager(me, name, forum);
        }

        public void register(string name, string last, string nick, string pass, string email)
        {
            //pipeProxy.Register(new Client { FirstName = name, LastName = last, Name = nick, PassWord = pass, Email = email });
            string regStr = "register " + name + " " + last + " " + nick + " " + pass + " " + email;
            pipeProxy.Login(regStr);
        }

        public void login(string flag, string user, string password)//flag for guest or login, user for user name
        {
            string loginMsg = flag + " " + user + " " + password;
            this.pipeProxy.Login(loginMsg);
        }

        internal void postReply(ClientRequest req)
        {
            this.pipeProxy.AddPost(req);
        }

        internal void newThread(ClientRequest req)
        {
            this.pipeProxy.AddThread(req);
        }

        internal void newSub(ClientRequest req)
        {
            this.pipeProxy.AddSubForum(req);
        }
        internal void getMainForum()
        {
            this.pipeProxy.GetForum(ClientRequest(forum));
        }

        internal void getSubForum(ClientRequest globalCommonClientRequest)
        {
            this.pipeProxy.GetSubForum(globalCommonClientRequest);
        }

        internal void getThread(ClientRequest globalCommonClientRequest)
        {
            this.pipeProxy.GetThread(globalCommonClientRequest);
        }

        //new requirments++++++++++++++++++++++++++++++++

        internal void exchangeModerator(string oldMod, string newMod, string forum)
        {
            this.pipeProxy.ExchangeModerator(oldMod, newMod, forum);
        }

        public void updateThread(ClientRequest req)//1 bet
        {
            this.pipeProxy.EditThread(req);
        }

        public void deleteThread(ClientRequest req)//1 gimel
        {
            this.pipeProxy.DeleteThread(req);
        }

        public void demote(User me, string name, string forum)//2 bet
        {
            this.pipeProxy.RemoveModerator(me, name, forum);
        }

        //what we do about replacement?

        public void deleteSubForum(ClientRequest req)//2 hei
        {
            this.pipeProxy.DeleteSubForum(req);
        }

        public void getForumStats(User me)//2 vav (OPTIONAL)
        {
            this.pipeProxy.GetForumStats(me, forum);
        }

        public void getForumsStats(User me)//3 (OPTIONAL)
        {
            this.pipeProxy.GetGlobalStats(me);
        }


        public void accessAdmin(Client me)
        {
            this.pipeProxy.GetAdminAccess(me);


        }



        internal void editThread(ClientRequest clientRequest)
        {
            this.pipeProxy.EditThread(clientRequest);
        }

        internal void editPost(ClientRequest clientRequest)
        {
            this.pipeProxy.EditPost(clientRequest);
        }
        // Callback-----------------------------------------------------------------------------

        public void OnMessageAdded(string message, DateTime timestamp)//wtf is this?
        {
            throw new NotImplementedException();
        }

        //public void receivePage(Common.DataBase.Page page)
        //{

        //    /*
        //    SortedDictionary<int, Common.DataBase.Message> messages = page.messages;
        //    string[] strArr = new string[10];
        //    foreach (int msg in messages.Keys)
        //    {
        //        Common.DataBase.Message tmpMsg = messages[msg];
        //        strArr[msg] = "(" + msg + ")" + "\tTitle: " + tmpMsg.Title + ":" + tmpMsg.Body;
        //    }
        //    */
        //    this.forumService.receivePage(page.getPageStrings(), page.header + "\n" + page.moderators);

        //}

        public void receiveJoin(User client, bool ans, string systemMsg)
        {
            //MessageBox.Show("got join");
            if (!ans)
            {
                new FormError("Illegal Username OR Password!").Show();
            }
            else
            {
                this.forumService.receiveJoin(client);
            }
        }

        public void receiveLogout()
        {

        }

        public void receiveNotify(string notifyMsg)
        {
            throw new NotImplementedException();
        }

        public void receiveStatistics(string statMsg)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {

        }

        public void receiveUsersList(List<string> uList)
        {
            this.forumService.receiveUsersList(uList);
        }

        public void autherizationError()
        {
            this.forumService.autherizationError();
        }

        internal void getAdminAccess(string user, string password)
        {
            this.pipeProxy.GetAdminAccess(user, password);
        }

        //new requirments++++++++++++++++++++++++++++++++

        public void receiveUpdateList(List<string> uList)//1 alef - check if we want to do 1 bet in here(server side implementation) 
        // or do it by getting a list of users to update from the server side
        {
            this.forumService.receiveUpdateList(uList);
        }

        //ALTERNATIVE
        public void receiveUpdateList(string uList)
        {
            //   this.forumService.receiveUpdateList(uList);
        }

        public void receiveAdminAccess(List<string> uList, string stats)
        {
            this.forumService.receiveAdminAccess(uList, stats);
        }

        public void receiveForum(Forum forum)
        {
            this.forumService.receiveForum(forum);
        }

        public void receiveSubForum(SubForum subforum)
        {
            this.forumService.receiveSubForum(subforum);
        }

        public void receiveThread(Thread thread)
        {
            this.forumService.receiveThread(thread);
        }


    }
}
